﻿using Payroll.BusinessLogic.Caching;
using Payroll.BusinessLogic.Data;
using Payroll.Common;
using Payroll.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Payroll.BusinessLogic.Devices
{
    public partial class DeviceService : IDeviceService
    {
        #region Fields

        /// <summary>
        /// Object context
        /// </summary>
        private readonly PayrollHRDataContext m_context;

        /// <summary>
        /// Cache manager
        /// </summary>
        private readonly ICacheManager _cacheManager;
       
        #endregion
        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="context">Object context</param>
        public DeviceService(PayrollHRDataContext context)
        {
            this.m_context = context;
            this._cacheManager = new PayrollRequestCache();
        }

        #endregion
        public int Save(Devices device)
        {
            var item = (from d in m_context.Devices
                        where d.DeviceID == device.DeviceID
                        select d).FirstOrDefault();
            if (!CommonHelper.IsNull(item))
            {
                item.DeviceCode = device.DeviceCode.ToUpper();
                item.DeviceName = device.DeviceName;
                item.Note = device.Note;
                item.Createby = device.Createby;
                item.Modified = device.Modified;
                item.CreateDate = device.CreateDate;
                item.ModifiedDate = device.ModifiedDate;
            }
            else
            {
                item = new Data.Device
                {
                    DeviceCode = device.DeviceCode.ToUpper(),
                    DeviceName = device.DeviceName,
                    Note = device.Note,
                    Createby = device.Createby,
                    Modified = device.Modified,
                    CreateDate = device.CreateDate,
                    ModifiedDate = device.ModifiedDate
                };
                m_context.Devices.InsertOnSubmit(item);
            }

            m_context.SubmitChanges();

            return item.DeviceID;
        }


        public PagedList<Devices> GetAllDevice(string deviceCode, string deviceName, int pageSize = int.MaxValue, int pageIndex = 0)
        {
            var query = from d in m_context.Devices
                        where d.Status == 0
                        select new Devices { DeviceID = d.DeviceID, DeviceCode = d.DeviceCode, DeviceName = d.DeviceName, Note = d.Note };
            if (!CommonHelper.IsNull(deviceCode))
            {
                query = query.Where(d => d.DeviceCode.ToLower().Contains(deviceCode.ToLower()));
            }
            if (!CommonHelper.IsNull(deviceName))
            {
                query = query.Where(d => d.DeviceCode.ToLower().Contains(deviceName.ToLower()));
            }
            return new PagedList<Devices>(query, pageIndex, pageSize);
        }


        public Devices GetByDeviceID(int deviceID)
        {
            var query = from d in m_context.Devices
                        where d.DeviceID == deviceID
                        select new Devices { DeviceID = d.DeviceID, DeviceCode = d.DeviceCode, DeviceName = d.DeviceName, Note = d.Note };
            return query.FirstOrDefault();
        }


        public PagedList<Devices> Delete(string deviceId, string deviceCode, string deviceName, int pageSize = int.MaxValue, int pageIndex = 0)
        {
            m_context.Devices_Delete(deviceId: deviceId);
            return this.GetAllDevice(deviceCode: deviceCode, deviceName: deviceName, pageSize: 100);
        }
    }
}
